package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.DamageListDao;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageListService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DamageListServiceImpl implements DamageListService {

    @Autowired
    private DamageListDao damageListDao;

    @Autowired
    private DamageListGoodsDao damageListGoodsDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private GoodsDao goodsDao;
    @Override
    public ServiceVO save(DamageList damageList, String damageListGoodsStr) {

        Gson gson = new Gson();
        List<DamageListGoods> damageListGoodsList = gson.fromJson(damageListGoodsStr, new TypeToken<List<DamageListGoods>>() {}.getType());
        User userByName = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        damageList.setUserId(userByName.getUserId());
        damageListDao.saveDamageList(damageList);

        damageListGoodsList.stream().peek(damageListGoods -> {
            damageListGoods.setDamageListId(damageList.getDamageListId());
            damageListGoodsDao.saveDamageListGoods(damageListGoods);
            Goods goods = goodsDao.getGoodsByGoodsId(damageListGoods.getGoodsId());
            goods.setInventoryQuantity(goods.getInventoryQuantity() - damageListGoods.getGoodsNum());
            goodsDao.update(goods);
        }).collect(Collectors.toList());

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        Map<String,Object> map = new HashMap<>();
        List<DamageList> damageLists = damageListDao.list(sTime,eTime);
        damageLists.stream().peek(damageList -> {
            User userByName = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
            damageList.setUserId(userByName.getUserId());
            damageList.setTrueName(userByName.getTrueName());
        }).collect(Collectors.toList());
        map.put("rows",damageLists);
        return map;
    }

    @Override
    public Map<String, Object> goodsList(Integer damageListId) {
        Map<String,Object> map = new HashMap<>();
        List<DamageListGoods> damageListGoodsList = damageListGoodsDao.goodsList(damageListId);
        map.put("rows",damageListGoodsList);
        return map;
    }
}
